<?php if ( ! DEFINED ( 'BASEPATH' ) ) die('No direct script access allowed!');

/**
 * @author 		Mark Skilbeck
 * @package 	PHP Project Tracker 0.0.1
 * @link		http://mahcuz.com/php-project-tracker
 *
 * This class provides access to the database information
 * concerning PPT 0.0.1.
 *
 * It provies basic getter and setter methods to retrieve and set
 * values the controller requests.
 */
 
class Theme extends Model {
	
	
	/**
	 * @var 	string		Contains the theme name.
	 * @access 	private
	 */
	var $_theme_name;
	/**
	 * @var 	string		Contains the theme authors name.
	 * @access	private
	 */
	var $_theme_author;
	/**
	 * @var 	string		Theme version. This could be INT type, but we shouldn't limit it to numbers.
	 * @access  private
	 */
	var $_theme_version;
	/**
	 * @var		string		Contains theme's location in FULL HTTP format (http://site.com/dir/to/css.css)
	 * @access	private
	 */
	var $_theme_location;
	
	/**
	 * @var   	string
	 */
	const ERR_NO_ACTIVE_THEME = 'There is no active theme.';
	/**
	 * @var		string
	 */
	const ERR_MLTPLE_ACTIVE_THEMES = 'There are multiple active themes.';
	
	/**
	 * Constructor sets the initial values of our private members for the current in-use 
	 * theme.
	 */
	function Theme ( )
	{
		# Call parent constructor
			parent::Model();
			
		# Initialize the Theme properties.
			$this->_set_all( );
	}
	
	/**
	 * Because our theme information properties are private
	 * and we don't provide an interface for their 'getting',
	 * the magic __call() method will be used to return the values.
	 * If $this->theme->name() is called, we return $this->_theme_name.
	 *
	 * @access private
	 */
	function __get ( $property )
	{
		# Edit the $method var to reflect our properties.
		$property = '_theme_' . $property;
		
		# No need to continue if the property doesn't exist.
		if ( ! isset ( $this->$propert ) ) 
			return;
		
		# Otherwise, return the value of the property.
		return $this->$property;
	}
	
	/**
	 * Set all member values.
	 * @access private
	 */
	function _set_all ( )
	{
		# Grab the active theme.
		$this->db
			 ->from('themes')
			 ->where('active', 1);
		# Store the result resource.
		$query = $this->db->get( );
		
		# We should always have a default (active = 1) theme stored in the database.
		if ( $query->num_rows( ) < 1 )
		{
			show_error('<strong>Theme model error</strong>: ' .
				self::ERR_NO_ACTIVE_THEME );
		}
		
		# There shouldn't be more than 1 active theme.
		if ( $query->num_rows( ) > 1 )
		{
			show_error('<strong>Theme model error</strong>: ' .
				self::ERR_MLTPLE_ACTIVE_THEMES );
		}
		
		# Load the result.
		$theme = $query->row();
		# Assign the values to our properties.
		$this->_theme_name 		= $theme->theme_name;
		$this->_theme_author 	= $theme->theme_author;
		$this->_theme_version	= $theme->theme_version;
		$this->_theme_location	= $theme->theme_location;
	}
	
}